/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package atm.gui;

import com.alee.extended.panel.GroupPanel;
import com.alee.extended.panel.WebCollapsiblePane;
import com.alee.extended.statusbar.WebMemoryBar;
import com.alee.extended.statusbar.WebStatusBar;
import com.alee.laf.StyleConstants;
import com.alee.laf.WebLookAndFeel;
import com.alee.laf.button.WebButton;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.rootpane.WebFrame;
import com.alee.laf.scroll.WebScrollPane;
import com.alee.laf.separator.WebSeparator;
import com.alee.laf.toolbar.ToolbarStyle;
import com.alee.laf.toolbar.WebToolBar;
import com.alee.managers.notification.NotificationIcon;
import com.alee.managers.notification.NotificationManager;
import com.alee.managers.tooltip.TooltipManager;
import com.alee.managers.tooltip.TooltipWay;

import atm.charts.AreaChartPanel;
import atm.charts.CategoryBarChartPanel;
import atm.connection.Connect;

import com.alee.extended.image.WebImage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.ScrollPaneConstants;

/**
 *
 * @author Quark
 */
public class Main extends WebFrame implements ActionListener {
    
    private WebPanel contentPane; // main content panel
    
    private WebToolBar toolBar;
    private WebButton addATMButton, deleteATMButton;
    private WebButton simulationButton, infoButton, routeButton, scheduleButton, refreshButton;
    
    private WebScrollPane centerScrollPane;
    private GroupPanel centerPanel;
    private WebPanel routePanel, schedulePanel;
    private ArrayList<WebCollapsiblePane> atmsPanelsList;
    // there should be a list of atms
    private WebCollapsiblePane atmCollapsiblePanel;
    private GroupPanel atmInfoPanel;
    
    private WebStatusBar statusBar;
    private WebMemoryBar memoryBar;
    
    private Connect connection;
    
    public Main(String title) {
        super(title);
        setIconImages(WebLookAndFeel.getImages());
        setDefaultCloseOperation(WebFrame.EXIT_ON_CLOSE);
        //setExtendedState(JFrame.MAXIMIZED_BOTH); // fullscreen
        
        createComponents();
        initActionListeners();
        
        pack();
        setLocationRelativeTo(null);
    }
    
    private void createComponents() {
        contentPane = new WebPanel();
        contentPane.setPreferredSize(new java.awt.Dimension(960, 600));
        contentPane.setUndecorated(true);
        
        contentPane.add(createToolBar(), BorderLayout.NORTH);
        contentPane.add(createCenterScrollPane(1), BorderLayout.CENTER); // default view is always a list of atms' detailed information
        contentPane.add(createStatusBar(), BorderLayout.SOUTH);
        add(contentPane);
    }
    
    private WebToolBar createToolBar() {
        toolBar = new WebToolBar(WebToolBar.HORIZONTAL);
        toolBar.setFloatable(false);
        toolBar.setToolbarStyle(ToolbarStyle.attached);

        simulationButton = new WebButton();
        simulationButton.setIcon(new ImageIcon(this.getClass().getResource("icons/start.png")));
        simulationButton.setPreferredSize(new java.awt.Dimension(64, 64));
        simulationButton.setRound(StyleConstants.smallRound);
        simulationButton.setRolloverShadeOnly(true);
        simulationButton.setRolloverDecoratedOnly(true);
        TooltipManager.setTooltip(simulationButton, "Simulate a withdrawal", TooltipWay.down, 0);
        
        addATMButton = new WebButton();
        addATMButton.setIcon(new ImageIcon(this.getClass().getResource("icons/plus.png")));
        addATMButton.setPreferredSize(new java.awt.Dimension(64, 64));
        addATMButton.setRound(StyleConstants.smallRound);
        addATMButton.setRolloverShadeOnly(true);
        addATMButton.setRolloverDecoratedOnly(true);
        TooltipManager.setTooltip(addATMButton, "Add new ATM to the list", TooltipWay.down, 0);

        deleteATMButton = new WebButton();
        deleteATMButton.setIcon(new ImageIcon(this.getClass().getResource("icons/minus.png")));
        deleteATMButton.setPreferredSize(new java.awt.Dimension(64, 64));
        deleteATMButton.setRound(StyleConstants.smallRound);
        deleteATMButton.setRolloverShadeOnly(true);
        deleteATMButton.setRolloverDecoratedOnly(true);
        deleteATMButton.setEnabled(false);
        TooltipManager.setTooltip(deleteATMButton, "Delete selected ATM // Does not and prolly never will work ^^", TooltipWay.down, 0);
        
        infoButton = new WebButton();
        infoButton.setIcon(new ImageIcon(this.getClass().getResource("icons/information.png")));
        infoButton.setPreferredSize(new java.awt.Dimension(64, 64));
        infoButton.setRound(StyleConstants.smallRound);
        infoButton.setRolloverShadeOnly(true);
        infoButton.setRolloverDecoratedOnly(true);
        TooltipManager.setTooltip(infoButton, "Repaint center panel to default information", TooltipWay.down, 0);
        
        routeButton = new WebButton();
        routeButton.setIcon(new ImageIcon(this.getClass().getResource("icons/route.png")));
        routeButton.setPreferredSize(new java.awt.Dimension(64, 64));
        routeButton.setRound(StyleConstants.smallRound);
        routeButton.setRolloverShadeOnly(true);
        routeButton.setRolloverDecoratedOnly(true);
        TooltipManager.setTooltip(routeButton, "Generate the optimal route the convoy should follow", TooltipWay.down, 0);

        scheduleButton = new WebButton();
        scheduleButton.setIcon(new ImageIcon(this.getClass().getResource("icons/table.png")));
        scheduleButton.setPreferredSize(new java.awt.Dimension(64, 64));
        scheduleButton.setRound(StyleConstants.smallRound);
        scheduleButton.setRolloverShadeOnly(true);
        scheduleButton.setRolloverDecoratedOnly(true);
        TooltipManager.setTooltip(scheduleButton, "Show the ATMs' filling schedule in new window", TooltipWay.down, 0);
        
        refreshButton = new WebButton();
        refreshButton.setIcon(new ImageIcon(this.getClass().getResource("icons/refresh.png")));
        refreshButton.setPreferredSize(new java.awt.Dimension(64, 64));
        refreshButton.setRound(StyleConstants.smallRound);
        refreshButton.setRolloverShadeOnly(true);
        refreshButton.setRolloverDecoratedOnly(true);
        refreshButton.setEnabled(false);
        TooltipManager.setTooltip(refreshButton, "Refresh all data [how about dispatching events?? xD]", TooltipWay.down, 0);

        toolBar.add(addATMButton);
        toolBar.add(deleteATMButton);
        toolBar.add(new WebSeparator(WebSeparator.VERTICAL));
        toolBar.add(simulationButton);
        toolBar.addToEnd(infoButton);
        toolBar.addToEnd(routeButton);
        toolBar.addToEnd(scheduleButton);
        toolBar.addToEnd(new WebSeparator(WebSeparator.VERTICAL));
        toolBar.addToEnd(refreshButton);

        return toolBar;
    }
    
    private WebScrollPane createCenterScrollPane(int view) {
        if (view == 1) centerScrollPane = new WebScrollPane(createCenterPanel(), false);
        else if (view == 2) centerScrollPane = new WebScrollPane(createRoutePanel(), false);
        else if (view == 3) centerScrollPane = new WebScrollPane(createSchedulePanel(), false);
        
        centerScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        centerScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        
        centerScrollPane.setBackground(null);
        
        return centerScrollPane;
    }
    
    private GroupPanel createCenterPanel() {
        centerPanel = new GroupPanel(7, false);
        centerPanel.setMargin(48);
        centerPanel.setUndecorated(true);
        centerPanel.setBackground(null);
        
        atmsPanelsList = new ArrayList<>();
        
        connection = new Connect();
        connection.getConnected();
        try {
			System.out.println(connection.prediction());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        ArrayList<String> atms = connection.getAtmsInfo();
        
        for (int i = 0; i < atms.size(); i++) {
            atmsPanelsList.add(createATMCollapsiblePanel(atms, i, connection.actualState10(i+1), connection.actualState20(i+1), connection.actualState50(i+1), connection.actualState100(i+1)));
        }
        
        centerPanel.add(atmsPanelsList);
        
        return centerPanel;
    }
    
    private WebCollapsiblePane createATMCollapsiblePanel(/*powinien dostać obiekt bankomatu chyba*/ArrayList<String> atm, int i, int w10, int w20, int w50, int w100) {
        final ImageIcon icon;
        
        // powinna ustawiać się ikona warning w sytuacji, gdy bankomat ma jakichś banknotów mniej niż określony próg
        //if (i % 9 == 0) 
        	icon = new ImageIcon(this.getClass().getResource("icons/settings.png"));
        //else icon = new ImageIcon(this.getClass().getResource("icons/warning.png"));
        
        atmCollapsiblePanel = new WebCollapsiblePane(icon, atm.get(i), createATMInfoPanel(i, w10, w20, w50, w100));
        atmCollapsiblePanel.setExpanded(false);
        atmCollapsiblePanel.setAnimate(true);
        atmCollapsiblePanel.setPreferredWidth(800);
        
        return atmCollapsiblePanel;
    }
    
    private GroupPanel createATMInfoPanel(int i, int w10, int w20, int w50, int w100) {
        WebLabel billsInfoLabel = new WebLabel("Specified bills information:", new ImageIcon(this.getClass().getResource("icons/linechartgradient.png")), WebLabel.CENTER);
        WebLabel historyInfoLabel = new WebLabel("Last week withdrawals information:", new ImageIcon(this.getClass().getResource("icons/linechart.png")), WebLabel.CENTER);
        WebLabel currentStateLabel = new WebLabel("Current state:", new ImageIcon(this.getClass().getResource("icons/information.png")), WebLabel.LEFT);
        
        CategoryBarChartPanel billsChartPanel = new CategoryBarChartPanel(i + 1);
        AreaChartPanel historyChartPanel = new AreaChartPanel(i + 1);
        ScheduleMiniPanel miniPanel = new ScheduleMiniPanel(null, w10, w20, w50, w100); // dać mu dane obiektu
                
        billsChartPanel.setPreferredSize(new java.awt.Dimension(320, 180));
        billsChartPanel.setUndecorated(true);
        
        historyChartPanel.setPreferredSize(new java.awt.Dimension(320, 180));
        historyChartPanel.setUndecorated(true);
        
        miniPanel.setPreferredSize(new java.awt.Dimension(80, 180));
        miniPanel.setUndecorated(true);
        
        // change the look of ScheduleMiniPanel to fit my needs: delete null header, distribute evenly and center
        miniPanel.setLayout(new GridLayout(5, 1));
        miniPanel.getNominal10Label().setHorizontalAlignment(WebLabel.CENTER);
        miniPanel.getNominal20Label().setHorizontalAlignment(WebLabel.CENTER);
        miniPanel.getNominal50Label().setHorizontalAlignment(WebLabel.CENTER);
        miniPanel.getNominal100Label().setHorizontalAlignment(WebLabel.CENTER);
        miniPanel.remove(0); // delete header to eliminate an empty line
        
        atmInfoPanel = new GroupPanel(64, false, new GroupPanel(32, true,
                                                    new GroupPanel(false, billsInfoLabel, billsChartPanel),
                                                    new GroupPanel(false, historyInfoLabel, historyChartPanel),
                                                    new GroupPanel(false, currentStateLabel, miniPanel)));
        atmInfoPanel.setMargin(15);
        
        return atmInfoPanel;
    }
    
    private WebPanel createRoutePanel() {
        routePanel = new WebPanel();
        routePanel.setBackground(Color.lightGray);
        routePanel.setMargin(0);
        
        //routePanel.add(new WebMultiLineLabel("Routing panel!! :D\n\nWydaje mi si�, �e najlepsz� opcj� b�dzie przekaza� do algorytmu list� bankomat�w\n" +
        //        "wymagaj�cych zaopatrzenia i ewentualnych szczeg��w [ile pieni�dzy w jakich nomina�ach]\ni wygenerowa� uporz�dkowan� list�, kt�r� " +
        //        "wyrysujemy na mapie...\n\n\n\nAlbo tylko uda�, �e sami to liczymy i przedstawi� w tym panelu wynik wygenerowany przez Google Maps... czyli embed a website in this panel. :)"));
        
        routePanel.add(new WebImage(new ImageIcon(this.getClass().getResource("map.png"))));
        
        return routePanel;
    }
    
    private WebPanel createSchedulePanel() {
        schedulePanel = new WebPanel();
        schedulePanel.setLayout(new GridLayout());
        schedulePanel.setBackground(Color.lightGray);
        schedulePanel.setMargin(24);
        
        /*
        ScheduleATMPanel powinien dostać listę bankomatów wymagających uzupełnienia danego dnia. Z tej listy wewnątrz klasy powinne być pobierane dane do ScheduleMiniPanel.
        */
        schedulePanel.add(new ScheduleATMPanel("Today", 0));
        schedulePanel.add(new ScheduleATMPanel("Tomorrow", 1));
        schedulePanel.add(new ScheduleATMPanel("+ 2", 2));
        schedulePanel.add(new ScheduleATMPanel("+ 3", 3));
        schedulePanel.add(new ScheduleATMPanel("+ 4", 4));
        
        return schedulePanel;
    }
    
    private WebStatusBar createStatusBar() {
        statusBar = new WebStatusBar();

        statusBar.addSpacing();

        final WebLabel statusBarLabel = new WebLabel("Copyright © 2013 Quark. All rights reserved.", new ImageIcon(this.getClass().getResource("icons/beta.png")));
        statusBar.addToEnd(statusBarLabel);

        statusBar.addSeparatorToEnd();

        memoryBar = new WebMemoryBar();
        memoryBar.setShowMaximumMemory(false);
        memoryBar.setPreferredWidth(memoryBar.getPreferredSize().width + 20);
        statusBar.addToEnd(memoryBar);

        NotificationManager.setMargin(0, 0, statusBar.getPreferredSize().height, 0);

        return statusBar;
    }
    
    private void initActionListeners() {
        addATMButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NotificationManager.showNotification("Funny story actually! You will need the logic implemented for this to work. xD", NotificationIcon.question.getIcon());
            }
        });
        
        simulationButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NotificationManager.showNotification("There's nothing to simulate yet...", NotificationIcon.error.getIcon());
            }
        });
        
        infoButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NotificationManager.showNotification("Does it work?? -^", NotificationIcon.tip.getIcon());
                contentPane.remove(centerScrollPane);
                contentPane.add(createCenterScrollPane(1), BorderLayout.CENTER);
                contentPane.repaint();
            }
        });
        
        routeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NotificationManager.showNotification("Generating the optimal route has not been implemented yet. :D", NotificationIcon.tip.getIcon());
                contentPane.remove(centerScrollPane);
                contentPane.add(createCenterScrollPane(2), BorderLayout.CENTER);
                contentPane.repaint();
            }
        });
        
        scheduleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NotificationManager.showNotification("<html>There is no such thing as schedule... <br />What are you talking about?? xD</html>");
                contentPane.remove(centerScrollPane);
                contentPane.add(createCenterScrollPane(3), BorderLayout.CENTER);
                contentPane.repaint();
            }
        });
        
        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NotificationManager.showNotification("What exactly would you like to refresh?? ;>", NotificationIcon.error.getIcon());
            }
        });
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
